package com.universityJob.modules.data.service.impl;

import com.alibaba.fastjson.JSON;
import com.universityJob.common.utils.*;
import com.universityJob.modules.data.dao.DataJobDao;
import com.universityJob.modules.data.entity.DataJobEntity;
import com.universityJob.modules.data.service.DataJobService;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.servlet.http.HttpServletResponse;


@Service("jobService")
public class DataJobServiceImpl extends ServiceImpl<DataJobDao, DataJobEntity> implements DataJobService {
    @Autowired
    DataJobDao dataJobDao;
    @Autowired
    private Upload upload;
    @Autowired
    private ExportExcel exportExcel;
    @Autowired
    private FormatData formatData;
    private Logger logger = LoggerFactory.getLogger(getClass());

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String jobname = (String) params.get("jobname");
        IPage<DataJobEntity> page = this.page(
                new Query<DataJobEntity>().getPage(params),
                new QueryWrapper<DataJobEntity>().
                        like(StringUtils.isNotBlank(jobname), "jobName", jobname)
        );

        return new PageUtils(page);
    }

    @Override
    public DataJobEntity queryByJobId(String jobid) {
        if (StringUtils.isNotBlank(jobid)) {
            DataJobEntity dataJobEntity = dataJobDao.selectJobByid(jobid);
            return dataJobEntity;
        }
        return null;
    }

    @Override
    public DataJobEntity selectJobByOid(String id) {
        try {
            DataJobEntity dataJobEntity = dataJobDao.selectJobByOid(id);
            return dataJobEntity;
        } catch (Exception e) {
        }
        return null;
    }

    @Override
    public R saveJobs(List<DataJobEntity> jobs) {
        boolean inserts = dataJobDao.insertJobs(jobs);
        if (inserts) {
            return R.ok();
        }
        return R.error();
    }

    @Override
    public R saveJob(DataJobEntity job) {
        boolean inserts = dataJobDao.insertJob(job);
        if (inserts) {
            return R.ok();
        }
        return R.error();
    }

    @Override
    public R updateJob(DataJobEntity job) {

        return null;
    }

    @Override
    public R deleteJob(Long[] jobIds) {
        boolean remove = this.removeByIds(Arrays.asList(jobIds));
        if (remove) {
            return R.ok();
        }
        return R.error("删除出错，请联系管理员！");
    }

    @Override
    public boolean truncateJob() {
        boolean b = dataJobDao.truncate();
        return b;
    }

    @Override
    public int exportStatisticsReport(HttpServletResponse response, Map<String, Object> params) {
        //获取数据
        List<Map<String, Object>> maps = dataJobDao.selectMaps(null);
        for (Map m : maps) {
            String areacodename = MapUtils.getString(m, "areacodename", "");
            String[] split = areacodename.split(",");
            boolean matches = split[0].matches("[0-9]{1,}");
            if (matches) {
                String s = formatData.codeToText(split);
                m.put("areacodename", s);
            }
        }
        exportExcel.excelInit(maps, response, params);
        return 1;
    }

    @Override
    public int inputExcel(HttpServletResponse response, Map<String, Object> params) {
        String jobs = MapUtils.getString(params, "job", "");
        int count = 0;
        List<DataJobEntity> list = JSON.parseArray(jobs, DataJobEntity.class);//list json字符串转
        for (DataJobEntity job : list) {
            job.setAreacodename(formatData.textToCode(job.getAreacodename()));
            job.setPublishdate(new Date());
            try {
                dataJobDao.insert(job);
                count += 1;
            } catch (Exception e) {
                logger.error("插入id为 {} 数据失败",job.getJobid());
            }
        }
        return count;
    }
}
